﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using SpLocalizer.TranslationProvider.Binq;

namespace SpLocalizer.TranslationProvider
{

    public class BingTranslator : AbstarctTranslationProvider
    {
        private LanguageServiceClient client = new LanguageServiceClient();

        private string _ApplicationToken = String.Empty;
        public BingTranslator(string ApplicationToken)
        {
            _ApplicationToken = ApplicationToken;
        }


        public override string TranslateString(string SourceText, System.Globalization.CultureInfo FromLanguage, System.Globalization.CultureInfo ToLanguage, string Format)
        {
            return client.Translate(_ApplicationToken, SourceText,
                   FromLanguage.Name,
                   ToLanguage.Name,
                   Format.Equals("Text") ? "text/plain" : "text/html",
                   "general");
        }

        public override string[] TranslateArray(string[] SourceText, System.Globalization.CultureInfo FromLanguage, System.Globalization.CultureInfo ToLanguage)
        {
            TranslateOptions options = new TranslateOptions(); // Use the default options 
            TranslateArrayResponse[] response =  client.TranslateArray(_ApplicationToken, SourceText,
                    FromLanguage.Name,
                    ToLanguage.Name, options);

            string[] returnValue = new string[response.Length];
            for (int i = 0; i < response.Length; i++)
                returnValue[i] = response[i].TranslatedText;

            return returnValue;
        }
    }
}
